
function askVerifyCode(a) {
    post_without_token("http://"+ip+"/auth/verify/send-code", {
            email: $("#input-email").val(),
            refer: a
        }, function (data) {
            window.alert(data.reason);
            $("#doAskVerifyCode").hide();
            $("#askVerifyCode").show();
        }
        , function () {
            $("#doAskVerifyCode").hide();
            $("#askVerifyCode").show();
        }
    )

}


function login() {
    post_without_token("http://"+ip+"/auth/verify/login", {
        username: $("#username").val(),
        password: $("#password").val(),
    }, function (data) {
        if (data.code === 200) {
            localStorage.setItem('token', data.t.token)
            window.location = "assignment.html"
        } else {
            alert(data.reason)
        }
        $("#login").show();
        $("#doLogin").hide();
    }, function () {
        $("#login").show();
        $("#doLogin").hide();
    })

}

function loginById() {
    post_without_token("http://"+ip+"/auth/verify/login-by-id", {
        id: $("#id").val(),
        password: $("#password").val(),
    }, function (data) {
        if (data.code === 200) {
            localStorage.setItem('token', data.t.token)
            window.location = "assignment.html"
        } else {
            alert(data.reason)
        }
        $("#login").show();
        $("#doLogin").hide();
    }, function () {
        $("#login").show();
        $("#doLogin").hide();
    })

}

function logout() {
    get("http://"+ip+"/auth/verify/logout", function (data) {

        window.location = "login.html"

    }, function () {
        window.location = "login.html"
    })
}


function register() {
    if ($("#password").val == $("#password1").val) {
        post_without_token('http://'+ip+'/auth/verify/register', {
                username: $("#username").val(),
                password: $("#password").val(),
                email: $("#input-email").val(),
                verifyCode: $("#verifyCode").val(),
                id: $("#sid").val(),
            },
            function (data) {
                if (data.code === 200) {
                    alert("注册成功！")
                    window.location = 'login.html'
                } else {
                    alert(data.reason)
                }
                $("#doRegister").hide();
                $("#register").show();
            }, function () {
                $("#doRegister").hide();
                $("#register").show();
            })
    }
    else {
        alert("您输入的两次密码不一致，请重新输入！")
        $("#doRegister").hide();
        $("#register").show();
    }
}

function update() {
    post_without_token("http://"+ip+"/auth/verify/update", {
        username: $("#username").val(),
        password: $("#password").val(),
        email: $("#input-email").val(),
        verifyCode: $("#verifyCode").val(),
        id: $("#sid").val(),
    }, function (data) {
        alert(data.reason)
        $("#doUpdate").hide();
        $("#update").show();
        window.location("login.html")

    }, function () {
        alert("服务器无响应");

        $("#doUpdate").hide();
        $("#update").show();
    })
}
function post_without_token(url, data, success, error) {
    $.ajax({
        type: "post",
        url: url,
        async: true,
        data: data,
        dataType: 'json',

        xhrFields: {
            withCredentials: true
        },
        success: success,
        error: error
    });
}

